home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / system / Audio.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  5.9 KB  |  185 lines

  1. " ----------------------------------------------------------------------- "
  2. " Audio Class implements control of the audio.device only, NOT any custom "
  3. " hardware you might have.                                                "
  4. ""
  5. "  WARNING:  You should know what you're doing to the Amiga OS before     "
  6. "            messing with this Class, or any other System Class!          "
  7. ""
  8. "  NOTE(S):  Methods marked 'BOOLEAN' return true on success, false on    "
  9. "            failure. "
  10. " ----------------------------------------------------------------------- "
  11.  
  12. Class Audio :Device ! private1 private2 private3 aChannel !
  13. [
  14.    initialize: portName channels: chBytes flags: flags priority: newPriority 
  15.       
  16.       " newPriority is in the range: -128 to 127.
  17.       *
  18.       * flags pertinent to the user are: 
  19.       *    ADIOF_SYNCCYCLE = 32 & ADIOF_NOWAIT = 64.
  20.       * 
  21.       * chbytes is a ByteArray that is only 4 bytes long.  #[ 3 5 10 12 ] 
  22.       * It is encoded as follows for each byte:
  23.       *
  24.       *    Channel 3 Channel 2 Channel 1 Channel 0 Allocation 
  25.       *      RIGHT     LEFT      LEFT      RIGHT      MASK
  26.       *
  27.       *        0         0         1         1        0x03    Stereo
  28.       *        0         1         0         1        0x05    Stereo
  29.       *        1         0         1         0        0x0A    Stereo
  30.       *        1         1         0         0        0x0C    Stereo
  31.       *
  32.       *        0         0         0         1        0x01    MonAural
  33.       *        0         0         1         0        0x02    MonAural
  34.       *        0         1         0         0        0x04    MonAural
  35.       *        1         0         0         0        0x08    MonAural
  36.       *  
  37.       * UNCOMMON USAGE:
  38.       *
  39.       *        0         0         0         0        0x00    NO CHANNELS
  40.       *        0         1         1         0        0x06    Both Left Ch.
  41.       *        0         1         1         1        0x07    Three Channels
  42.       *        1         0         0         1        0x09    Both Right Ch.
  43.       *        1         0         1         1        0x0B    Three Channels
  44.       *        1         1         0         1        0x0D    Three Channels
  45.       *        1         1         1         0        0x0E    Three Channels
  46.       *        1         1         1         1        0x0F    ALL Channels
  47.       "
  48.  
  49.       private1 <- <primitive 220 1>.
  50.  
  51.       private2 <- <primitive 220 3 private1 portName flags newPriority chBytes>.
  52.       
  53.       (self openChannel: chBytes priority: newPriority)
  54.         ifFalse: [self error: 'Could NOT open Audio channel(s)!']
  55. |
  56.    openChannel: chByteArray priority: pri ! rval !     " BOOLEAN "
  57.  
  58.       rval     <- <primitive 220 21 private1 pri chByteArray>.
  59.  
  60.       aChannel <- self myChannel.
  61.  
  62.       ^ rval
  63. |
  64.    myChannel
  65.  
  66.       ^ <primitive 220 27 private1>
  67. |
  68.    volume: volumeLevel                                 " BOOLEAN "
  69.  
  70.       ^ <primitive 220 14 private1 volumeLevel>     
  71. |
  72.    period: newPeriod                                   " BOOLEAN "
  73.  
  74.       " The frequency of the sound you wish to play is 
  75.       * dependent on the period & the length of the sound
  76.       * data.  The frequency is determined by dividing the
  77.       * VideoClockFreq by the product of the period & data 
  78.       * length.  
  79.       *
  80.       * Example:  f = 3579545 / (256 * period) for NTSC
  81.       *           f = 4436618 / (256 * period) for PAL
  82.       * 
  83.       *           where 256 is the length of the data.
  84.       "      
  85.       ^ <primitive 220 13 private1 newPeriod>     
  86. |
  87.    waitCycle                                           " BOOLEAN " 
  88.  
  89.       ^ <primitive 220 16 private1 aChannel>
  90. |
  91.    read
  92.  
  93.       ^ <primitive 220 17 private1 aChannel>
  94. |
  95.    setData: aByteArray
  96.  
  97.       private3 <- <primitive 220 26 private1 aByteArray>
  98. |
  99.    playAt: volume for: duration                        " BOOLEAN "
  100.  
  101.       " if the duration is zero, the channel will play 
  102.       * continuously, until you abort it (stop method).
  103.       * the number of cycles is dependent on the duration &
  104.       * the frequency (determined by the period & data length).
  105.       *
  106.       *   Example: numCycles = (frequency * duration) / 1000
  107.       *
  108.       * See the 'period:' method for frequency calculation. 
  109.       "
  110.       ^ <primitive 220 15 private1 volume duration aChannel>
  111. |
  112.    start                                               " BOOLEAN "
  113.  
  114.       ^ <primitive 220 12 private1>
  115. |
  116.    stop                                                " BOOLEAN "
  117.  
  118.       ^ <primitive 220 11 private1>
  119. |
  120.    reset                                               " BOOLEAN "
  121.  
  122.       ^ <primitive 220 10 private1>
  123. |
  124.    changePriority: newPriority                         " BOOLEAN "
  125.  
  126.       ^ <primitive 220 6 private1 newPriority>
  127. |
  128.    flush                                               " BOOLEAN "
  129.  
  130.       ^ <primitive 220 9 private1>
  131. |
  132.    clear                                               " BOOLEAN "
  133.  
  134.       ^ <primitive 220 24 private1>
  135. |
  136.    update                                              " BOOLEAN "
  137.  
  138.       ^ <primitive 220 25 private1>
  139. |
  140.    finish                                              " BOOLEAN "
  141.  
  142.       ^ <primitive 220 8 private1>
  143. |
  144.    lock                                                " BOOLEAN "
  145.  
  146.       ^ <primitive 220 7 private1>
  147. |
  148.    read: audioFileName size: size                      " BOOLEAN "
  149.  
  150.       ^ <primitive 220 18 private1 audioFileName size>
  151. |
  152.    write: audioFileName size: size                     " BOOLEAN "
  153.  
  154.       ^ <primitive 220 19 private1 audioFileName size>
  155. |
  156.    freeChannel
  157.  
  158.       " Tell the system you are done with an Audio Channel: "
  159.       <primitive 220 20 private1 private2 aChannel>
  160. |
  161.    disposeData
  162.  
  163.       <primitive 220 2 private3>.
  164.  
  165.       <primitive 250 5 0 private3>.
  166.  
  167.       ^ nil
  168. |
  169.    dispose
  170.  
  171.       <primitive 220 0 private1 private2>.
  172.  
  173.       self disposeData.
  174.  
  175.       <primitive 220 5 private1>.
  176.  
  177.       <primitive 250 5 0 private1>. " One-way ticket to death!! "
  178.  
  179.       ^ nil
  180. |
  181.    audioKey
  182.  
  183.       ^ <primitive 220 22 private1> " NOT really needed at this time. "
  184. ]
  185.